package com.itheima.controller;

import com.itheima.common.PageBean;
import com.itheima.common.R;
import com.itheima.dto.QueryStudentDto;
import com.itheima.pojo.Student;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/students")
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 添加学生
     */
    @PostMapping
    public R save(@RequestBody Student student) {
        studentService.save(student);
        return R.ok();
    }

    /**
     * 条件分页查询
     */
    @GetMapping
    public R page(QueryStudentDto dto) {
        PageBean pageBean = studentService.page(dto);
        return R.ok(pageBean);
    }

    /**
     * 根据ID查询学生信息
     */
    @GetMapping("/{id}")
    public R getInfo(@PathVariable Integer id) {
        Student student = studentService.getInfo(id);
        return R.ok(student);
    }

    /**
     * 修改学生信息
     */
    @PutMapping
    public R update(@RequestBody Student student) {
        studentService.update(student);
        return R.ok();
    }

    /**
     * 删除学生信息
     */
    @DeleteMapping("/{ids}")
    public R delete(@PathVariable List<Integer> ids) {
        studentService.delete(ids);
        return R.ok();
    }

    /**
     * 违纪处理
     */
    @PutMapping("/violation/{id}/{score}")
    public R violationHandle(@PathVariable Integer id, @PathVariable Integer score) {
        studentService.violationHandle(id, score);
        return R.ok();
    }
}
